#include "PositionKeeper.h"
#include "PortalOfPowerSpace.h"
#include "SentinelBridgeSpace.h"
#include "..\Character\PlayerCharacter.h"
#include "Space.h"

PositionKeeper::PositionKeeper(){}

talisman_map::Space * PositionKeeper::advanceCharacter(PlayerCharacter * iCharacter, unsigned int iNumberOfSpaces)
{
	int moves = (iCharacter->isToad() ? 1 : iNumberOfSpaces);

	talisman_map::Space * wSpace = getCharacterLocation(iCharacter);
	for (unsigned int i = 0; i < moves; ++i)
	{
		wSpace = wSpace->getNextSpace();
	}
	mPositions[iCharacter] = wSpace;
	return wSpace;
}

talisman_map::Space * PositionKeeper::movebackCharacter(PlayerCharacter * iCharacter, unsigned int iNumberOfSpaces)
{
	int moves = (iCharacter->isToad() ? 1 : iNumberOfSpaces);

	talisman_map::Space * wSpace = getCharacterLocation(iCharacter);

	for (unsigned int i = 0; i < moves; ++i)
	{
		wSpace = wSpace->getPrevSpace();
	}
	mPositions[iCharacter] = wSpace;
	return wSpace;
}

talisman_map::Space * PositionKeeper::takeTheBridge(PlayerCharacter * iCharacter)
{
	talisman_map::Space * wSpace = getCharacterLocation(iCharacter);

	talisman_map::SentinelBridgeSpace * wBridge = dynamic_cast<talisman_map::SentinelBridgeSpace *>(wSpace);
	
	if (0 != wBridge)
	{
		mPositions[iCharacter] = wBridge->getSentinelBridgeOtherEnd();
		return wBridge->getSentinelBridgeOtherEnd();
	}
	else
	{
		return wSpace;
	}
}

talisman_map::Space * PositionKeeper::useThePortal(PlayerCharacter * iCharacter)
{
	talisman_map::Space * wSpace = getCharacterLocation(iCharacter);

	talisman_map::PortalOfPowerSpace * wPortal = dynamic_cast<talisman_map::PortalOfPowerSpace *>(wSpace);

	if (0 != wPortal)
	{
		mPositions[iCharacter] = wPortal->getPortalOfPowerOtherEnd();
		return wPortal->getPortalOfPowerOtherEnd();
	}
	else
	{
		return wSpace;
	}
}